package com.xsoft.demo6;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.File;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

public class PictureActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int PERMISSION_WRITE_EXTERNAL_STORAGE = 10;
    private static final int PERMISSION_READ_EXTERNAL_STORAGE = 11;
    private static final int PERMISSION_READ_EXTERNAL_STORAGE_PICK_PICTURE = 12;
    private static final int REQUEST_PICK_PICTURE = 1;
    private ImageView pickPicture;
    private ImageView picture;
    private String filePath;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);
        pickPicture = findViewById(R.id.picture_img);
        pickPicture.setOnClickListener(this);
        findViewById(R.id.write).setOnClickListener(this);
        findViewById(R.id.read).setOnClickListener(this);
        picture = findViewById(R.id.picture);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.picture_img:
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                        == PackageManager.PERMISSION_GRANTED) {
                    pickPicture();
                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_READ_EXTERNAL_STORAGE_PICK_PICTURE);
                    }
                }
                break;
            case R.id.write:
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        == PackageManager.PERMISSION_GRANTED) {
                    write();
                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,}, PERMISSION_WRITE_EXTERNAL_STORAGE);
                    }
                }
                break;
            case R.id.read:
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                        == PackageManager.PERMISSION_GRANTED) {
                    read();
                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_READ_EXTERNAL_STORAGE);
                    }
                }
                break;
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_WRITE_EXTERNAL_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                write();
                return;
            }
        } else if (requestCode == PERMISSION_READ_EXTERNAL_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                read();
                return;
            }
        } else if (requestCode == PERMISSION_READ_EXTERNAL_STORAGE_PICK_PICTURE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                pickPicture();
                return;
            }
        }
        Toast.makeText(this, "需要授权应用读写权限", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_PICK_PICTURE) {
            Uri uri = data.getData();
            filePath = Utils.getFilePathByUri(this, uri);
            if (!TextUtils.isEmpty(filePath)) {
                Glide.with(this).load(filePath).into(pickPicture);
            }
        }
    }

    private void pickPicture() {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, REQUEST_PICK_PICTURE);
    }

    private void write() {
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        Utils.writePicture(filePath);
    }

    private void read() {
        String filePath = Utils.readPicture();
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        File file = new File(filePath);
        if (!file.exists()) {
            return;
        }
        Glide.with(this).load(filePath).into(picture);
    }
}
